package my.peerencrypt.utils;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

/**
 * @author ZhangXiang
 * 2010-6-8
 */
public class SimpleDataSource implements DataSource {

	private Logger logger = Logger.getLogger(SimpleDataSource.class);
	
	private String driverClassName;
	
	private String url;

	private String username;

	private String password;
	
	public SimpleDataSource(String driverClassName,String url,
			String username, String password) {
		setDriverClassName(driverClassName);
		setUrl(url);
		setUsername(username);
		setPassword(password);
	}

	public SimpleDataSource(String driverClassName, String url) {
		setDriverClassName(driverClassName);
		setUrl(url);
	}

	@Override
	public Connection getConnection() throws SQLException {
		return this.getConnection(username, password);
	}

	@Override
	public Connection getConnection(String username, String password)
			throws SQLException {
		try{
			return DriverManager.getConnection(url, username, password);
		}catch(SQLException e){
			logger.error("connect Database error:url="+url+",username="+username+",password"+password);
			throw e;
		}
	}

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		throw new UnsupportedOperationException("getLogWriter");
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		return 0;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		throw new UnsupportedOperationException("setLogWriter");
	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		throw new UnsupportedOperationException("setLoginTimeout");
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return DataSource.class.equals(iface);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		if (iface == null) {
			throw new IllegalArgumentException("Interface argument must not be null");
		}
		if (!DataSource.class.equals(iface)) {
			throw new SQLException("DataSource of type [" + getClass().getName() +
					"] can only be unwrapped as [javax.sql.DataSource], not as [" + iface.getName());
		}
		return (T) this;
	}

	
	public static ClassLoader getDefaultClassLoader() {
		ClassLoader cl = null;
		try {
			cl = Thread.currentThread().getContextClassLoader();
		}
		catch (Throwable ex) {
		}
		if (cl == null) {
			cl = SimpleDataSource.class.getClassLoader();
		}
		return cl;
	}
	
	public String getDriverClassName() {
		return driverClassName;
	}

	public void setDriverClassName(String driverClassName) {
		String driverClassNameToUse = driverClassName.trim();
		try {
			Class.forName(driverClassNameToUse, true, getDefaultClassLoader());
		} catch (ClassNotFoundException e) {
			throw new IllegalStateException("Could not load JDBC driver class [" + driverClassNameToUse + "]", e);
		}
		this.driverClassName = driverClassNameToUse;
		logger.debug("Loaded JDBC driver: " + driverClassNameToUse);
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
}
